/**
 * Copyright (c) 2018-2021 ThunderSoft
 * All Rights Reserved by Thunder Software Technology Co., Ltd and its affiliates.
 * You may not use, copy, distribute, modify, transmit in any form this file
 * except in compliance with ThunderSoft in writing by applicable law.
 */

package com.kll.tv.base.views;

import android.content.Intent;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.kll.carolinehmibiz.OperationManager;
import com.kll.carolinehmibiz.model.AccountModel;
import com.kll.carolinehmibiz.model.OperateListener;
import com.kll.carolinehmibiz.net.bean.GetSystemMenuResponse;
import com.kll.carolinehmibiz.net.bean.RefreshTokenResponse;
import com.kll.carolinehmibiz.presentation.BasePresenter;
import com.kll.tv.utils.AppToast;
import com.kll.tv.views.LoginActivity;

/**
 * BaseFragment.
 *
 * @author: jiangwei
 * @Date: 18-12-21 PM3:17
 */
public abstract class MainBaseFragment<T extends BasePresenter> extends BaseFragment<T> {

    protected GetSystemMenuResponse mMenuData;

    public void setMenuData(GetSystemMenuResponse menuData){
        mMenuData = menuData;
    }

    /**
     * 刷新token
     */
    public void refreshToken() {
        // token过期 刷新token
        OperationManager.Companion.getInstancce().refreshToken(new OperateListener() {
            @Override
            public void onSuccess(int state, @Nullable Object obj) {
                RefreshTokenResponse data = (RefreshTokenResponse) obj;
                AccountModel.Companion.getInstance(getContext()).updateUserToken(data.getToken());
            }

            @Override
            public void onFail(int code, @NonNull String message) {
                AppToast.show(message, 2000);
                if (code == 101) {
                    loginAgain();
                }
            }
        });
    }

    /**
     * 重新登录
     */
    public void loginAgain() {
        // 跳转登录页面重新登录
        OperationManager.Companion.getInstancce().logout();
        startActivity(new Intent(getContext(), LoginActivity.class));
    }

}
